package com.zoke.daycarpool.view;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

import com.zoke.daycarpool.R;
import com.zoke.support.base.BaseActivity;
import com.zoke.support.floatingmenu.FloatingItem;
import com.zoke.support.floatingmenu.FloatingMenu.DismissListener;
import com.zoke.support.floatingmenu.FloatingMenu.FloatingItemClick;
import com.zoke.support.floatingmenu.FloatingMenu.MenuType;
import com.zoke.support.utils.CommonUtil;

/**
 * 浮动的框
 * 
 * @author jack
 * 
 */
public class PullMenu implements OnClickListener {
	private Context mContext;
	private LayoutInflater mInflater;
	private List<FloatingItem> mParentList;
	private int mMenuHeight;
	private int mMenuWidth;
	private ListView mListView;
	private PopupWindow mPopupWindow;
	private PullMenuAdapter mAdapter;
	private FloatingItem mParentItem;
	private FloatingItemClick mOnItemClickListener;

	/** 传Acitivity引用进来 **/
	public PullMenu(Context context, List<FloatingItem> list) {
		// 默认为1级列表
		this.mContext = context;
		mInflater = LayoutInflater.from(mContext);
		mMenuHeight = CommonUtil.getWindowHeight((BaseActivity) mContext) / 3;
		mMenuWidth = CommonUtil.getWindowWidth((BaseActivity) mContext) / 2;
		this.mParentList = list;
		this.prepare();
	}

	private DismissListener listener;

	public void setDismiss(DismissListener listen) {
		this.listener = listen;
	}

	public void notifyDataChanged() {
		if (mAdapter != null)
			mAdapter.notifyDataSetChanged();
	}

	/** 准备 **/
	private void prepare() {
		View mContentView = mInflater.inflate(R.layout.customview_pullmenu,
				null);
		mListView = (ListView) mContentView.findViewById(R.id.pullmenu);
		mContentView.setOnClickListener(this);
		mListView.getLayoutParams().height = mMenuHeight;
		mListView.getLayoutParams().width = mMenuWidth;
		mAdapter = new PullMenuAdapter(mContext, mParentList);
		mPopupWindow = new PopupWindow(mContentView,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		// 外部可触摸
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow.setFocusable(true);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mParentItem = mParentList.get(position);
				changeParentSelected(position);
				if (mOnItemClickListener != null)
					mOnItemClickListener.onFloatingItemClick(position,
							MenuType.ONEMENU, mParentItem);

				dismiss();
			}
		});
		mPopupWindow.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				if (listener != null)
					listener.onDismiss();
			}
		});
	}

	/** 改变父类的选中项 **/
	private void changeParentSelected(int position) {
		for (int x = 0; x < mParentList.size(); x++) {
			FloatingItem item = mParentList.get(x);
			if (x == position) {
				item.setSelected(true);
			} else {
				item.setSelected(false);
			}
		}
	}

	/** 设置条目监听 **/
	public void setFloatingItemClick(FloatingItemClick listener) {
		this.mOnItemClickListener = listener;
	}

	@Override
	public void onClick(View v) {
		this.dismiss();
	}

	/**
	 * 漂浮在anchor下面
	 * 
	 * @param anchor
	 */
	public void showAsDropDown(View anchor) {
		if (null != mPopupWindow) {
			mPopupWindow.showAsDropDown(anchor);
		}
	}

	/**
	 * 漂浮在anchor下据x、y轴xoff、yoff距离
	 * 
	 * @param anchor
	 * @param xoff
	 * @param yoff
	 */
	public void showAsDropDown(View anchor, int xoff, int yoff) {
		if (null != mPopupWindow) {
			mPopupWindow.showAsDropDown(anchor, xoff, yoff);
		}
	}

	/** 隐藏 菜单 **/
	public void dismiss() {
		if (null != mPopupWindow) {
			mPopupWindow.dismiss();
		}
	}
}
